import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'model/user.dart';

part 'global_state.dart';

class GlobalCubit extends Cubit<GlobalState> {
  GlobalCubit() : super(GlobalState(user: User()));

  init() async {
    var sp = await SharedPreferences.getInstance();
    debugPrint(sp.getString('user'));
    state.user = User.fromJson(sp.getString('user'));
    emit(state.clone());
  }

  setUser(User user) async {
    state.user = user;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString('user', state.user.toString());
    emit(state.clone());
  }
}
